package com.foreveross.taskservice.common.mq.destination;

import java.util.HashMap;
import java.util.Map;

import javax.jms.Destination;

import com.foreveross.taskservice.common.CacheDataSpace;
import com.foreveross.taskservice.common.mq.QueueNameBuild;

/**
 * 一个简易的MQ目的工厂
 * 主要根据不同的渠道缓存不同的Destination
 * 以后可以加入不同的优先级队列的区分
 * @author Administrator
 *
 */
public class DestinationFactory {
	private static DestinationFactory instace;
	private Map<String,javax.jms.Destination> cache;
	private DestinationFactory(){
		cache=new HashMap<String,javax.jms.Destination>();
	};
	public synchronized static DestinationFactory getInstance(){
		if(instace==null){
			instace=new DestinationFactory();
		}
		return instace;
	}
	
	public javax.jms.Destination getChannelDestination(CacheDataSpace space,long channelId){
		return getQueueDestination(QueueNameBuild.buildTaskQueueName(space, channelId));
	}
	
	public javax.jms.Destination getRollbackDestination(CacheDataSpace space){
		return getQueueDestination(QueueNameBuild.buildRollQueueName(space));
	}
	
	public synchronized javax.jms.Destination getQueueDestination(String name){
		if(!cache.containsKey(name)){
			Destination d=new org.apache.activemq.command.ActiveMQQueue(name);
			cache.put(name, d);
		}
		return cache.get(name);
	}
	
	public Map<String, javax.jms.Destination> getAllDestination(){
		return cache;
	}
	
	public synchronized void remove(String name){
		if(cache != null){
			cache.remove(name);
		}
	}
	
}
